package com.aircms.service.impl;

import com.aircms.common.ServerResponse;
import com.aircms.dao.RolePermissionMapper;
import com.aircms.pojo.RolePermission;
import com.aircms.pojo.vo.RolePermissionVo;
import com.aircms.service.IRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author air
 * @create 2018-06-27 下午4:05
 */
@Service("iRolePermissionService")
public class RolePermissionServiceImpl implements IRolePermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private RolePermission rolePermission;

    /**
     * 获取角色的路由权限
     * @param roleIds
     * @return
     */
    public ServerResponse getRoleRouterPermissionIds(String[] roleIds){
        List<RolePermissionVo> rolePermissions= rolePermissionMapper.getRolePermissionIdsByRoleIs(roleIds,1);
        rolePermissions.forEach(item->{
            System.out.println(item.getRoleId()+""+item.getId());
        });
        return  ServerResponse.resResponse(rolePermissions);
    }

    /**
     * 获取角色的菜单权限
     * @param roleIds
     * @return
     */
    public ServerResponse getRoleMenuPermissionIds(String[] roleIds){
        List<RolePermissionVo> rolePermissions= rolePermissionMapper.getRolePermissionIdsByRoleIs(roleIds,2);
        rolePermissions.forEach(item->{
            System.out.println(item.getRoleId()+""+item.getId());
        });
        return  ServerResponse.resResponse(rolePermissions);
    }


    /**
     * 添加角色权限
     * @param rolePermissionVo
     * @return
     */
    public ServerResponse store(RolePermissionVo rolePermissionVo){
        rolePermission.setRoleId(rolePermissionVo.getRoleId());
        // 删除所角色所有权限
        rolePermissionMapper.deleteByRoleId(rolePermissionVo.getRoleId());

        // 添加路由权限
        rolePermissionVo.getRouterPermission().forEach(item->{
            rolePermission.setPermissionId(item.getId());
            rolePermissionMapper.insertSelective(rolePermission);
        });

        // 添加菜单权限
        rolePermissionVo.getMenuPermission().forEach(item->{
            rolePermission.setPermissionId(item.getId());
            rolePermissionMapper.insertSelective(rolePermission);
        });
        return ServerResponse.resResponse();
    }


    /**
     * 删除角色权限
     * @param id
     * @return
     */
    public ServerResponse destroy(Integer id){
        if(rolePermissionMapper.deleteByPrimaryKey(id)<1){
            return ServerResponse.resResponse("",-2,"id 不存在");
        }

        return ServerResponse.resResponse();
    }


}
